﻿//get请求
function getHttp(url, obj, callback) {
    //url是请求地址
    //obj是请求参数
    //callback是回调掉参数
    Vue.http.get(url, { params: obj })
        .then(function (res) {
            callback(res.body)
        }, function (err) {
            console.log(err)
        })
}
//post请求
function postHttp(url, obj, callback) {
    Vue.http.post(url, obj)
        .then(function (res) {
            console.log(res)
            callback(res)
        }, function (err) {
            console.log(err)
        })
}
//table的查询条件
function Flip(f_dom, c_dom, action, obj, flip,kw_dom) {
    f_dom.on(action, c_dom, function () {
        var count = Pagination.Count;
        if (action == "change") {
            if ($(this).hasClass("jumpPage")) {
                if ($(this).val().trim() == "") {
                    $(this).val($(".pagination .active").text());
                }else if ($(this).val().trim() % 1 == 0) {
                    if ($(this).val().trim() > count) {
                        $(this).val(count);
                    }
                    if ($(this).val().trim() < 1) {
                        $(this).val(1);
                    }
                    obj[flip] = $(this).val();
                } else {
                    showTipBox($("#opeTips"), "请输入有效数字");
                    $(this).val($(".pagination .active").text());
                }
            } else {
                if ($(this).attr("id") == "page") {
                    Pagination.pageSize = $(this).val();
                }
                obj.CurrentIndex = 1;
                obj[flip] = $(this).val();
            }
        } else {
            if ($(this).is("li")) {
                if ($(this).hasClass("first")) {
                    obj[flip] = 1;
                } else if ($(this).hasClass("last")) {
                    obj[flip] = Pagination.Count;
                } else {
                    obj[flip] = $(this).text();
                }
            } else {
                obj.CurrentIndex = 1;
                if (flip instanceof Array) {
                    if (kw_dom) {
                        for (var i = 0; i < flip.length; i++) {
                            obj[flip[i]] = $(kw_dom[i]).val().trim();
                        }
                    }
                } else {
                    if (kw_dom) {
                        obj[flip] = $(kw_dom).val().trim();
                    }
                }
                
            }
        }
        changeHashUrl(obj);
    })
}
function hashchange(obj, count, fn) {
    for (keys in obj) {
        obj[keys] = GetQueryStringH(keys);
    }
    if (obj.CurrentIndex > count || obj.CurrentIndex < 1) {
        obj.CurrentIndex = setCurrentIndex(count, 1, GetQueryStringH("CurrentIndex"));
        changeHashUrl(obj);
    }
    fn();
}
function changeHashUrl(obj) {
    var urlStr = "#";
    for (param in obj) {
        urlStr += param + "=" + obj[param] + "&";
    }
    urlStr = urlStr.slice(0, urlStr.length - 1);
    window.location.hash = urlStr;
}
//只能输入数字函数
function onlyNumber(event) {
    var keyCode = event.keyCode;
    if (keyCode < 48 || keyCode > 57) {
        event.keyCode = 0;
    }
}

$(function () {
    $('.form_datetime').datetimepicker({
        format: "yyyy-mm-dd hh:ii",
        language:"zh-CN",
        showMeridian: true,
        autoclose: true,
        todayBtn: false,
    });

   
    $(document).on("click", ".empty", function () {
        $(this).removeClass("empty");
    });

    /**
     *yaols  20180409   结束时间   将小于开始时间置灰
     */
    //$("#endTime").on("changeDate", function () {
    //    if (new Date($(this).val().replace(/-/g, "/")) < new Date($("#startTime").val().replace(/-/g, "/")) || $("#startTime").val() == "") {
    //        $(this).val("");
    //        alert("没有开始时间或开始时间大于结束时间");
    //        return false;
    //    }
    //})
    //$('#startTime').datetimepicker('setStartDate', new Date());  
    $("#startTime").on("changeDate", function (event) {
        //event.preventDefault();
        //event.stopPropagation();
        var startTime = event.date;
        $('#endTime').datetimepicker('setStartDate', startTime);  
    })
})

function showTipBox(obj,tipInfo,fn) {
    $(obj).html(tipInfo);
    setTimeout(function () {
        $(obj).slideDown();
    }, 1000);
    setTimeout(function () {
        $(obj).slideUp();
        if (fn) {
            fn();
        }
    }, 2000);
}
function showTipModal(tipInfo) {
    $("#showTipModal .confirmTipInfo").html(tipInfo);
}
function getCurDate() {
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth() + 1;
    var dates = today.getDate() + 2;
    var str = year + "-" + month + "-" + dates;
    return str;
}
function getWeekDate() {
    var today = new Date();
    var date7 = new Date(today.getTime() - 24 * 60 * 60 * 7 * 1000);
    var year = date7.getFullYear();
    var months = date7.getMonth() + 1;
    var dates = date7.getDate();
    var str = year + "-" + months + "-" + dates;
    return str;
}
//判断所有的查询条件是否为空
var isNull = false;
var num = 0;
var onOffSel = false;
var onOffTime = false;
var onOffKeyword = false;
function selIsNull() {
    num = 0;
    var length = $(".query").find("select").not($("#provinceList")).length;
    $(".query").find("select").not($("#provinceList")).each(function (item) {
        if ($(this).val() == "-1") {
            console.log($(this))
            num++;
        }
    })
    if (num === length) {
        onOffSel = true;
    } else {
        onOffSel = false;
    }
    return onOffSel;
}
function timeIsNull() {
    if ($("#startTime").val() == "" && $("#endTime").val() == "") {
        onOffTime = true;
    } else {
        onOffTime = false;
    }
    return onOffTime;
}

function keywordIsNull() {
    return $("#keyWord").val().trim() == "";
}

function searchIsNull() {
    if (selIsNull() && timeIsNull() && keywordIsNull()) {
        isNull = true;
    } else {
        isNull = false;
    }
    return isNull;
}
function ope() {
    if (searchIsNull()) {
        obj.StartTime = getWeekDate();
        obj.EndTime = getCurDate();
    } else {
        if (!timeIsNull()) {
            obj.StartTime = $("#startTime").val();
            obj.EndTime = $("#endTime").val();
        } else {
            obj.StartTime = "";
            obj.EndTime = "";
        }
    }
}
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]); return null;
}
function GetQueryStringH(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.hash.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]); return null;
}
function setCurrentIndex(max,min,currentIndex) {
    if (currentIndex > max){
        currentIndex = max;
    }
    if (currentIndex < min) {
        currentIndex = min;
    }
    return currentIndex;
}
function initUrl() {
    var url = window.location.href;
    var search = window.location.search;
    var hash = window.location.hash;
    var urlNew = "";
    if (search) {
        urlNew = url.split("?")[0];
    } else {
        if (hash) {
            urlNew = url.split("#")[0];
        }
    }
    window.location.href = urlNew;
}
//排序
function arrSort(arr, sortBy,sortName) {
    if (sortBy == "asc") {
        arr.sort(function (p1, p2) {
            return p1[sortName] < p2[sortName] ? -1 : 1;
        });
    }else if (sortBy == "desc") {
        arr.sort(function (p1, p2) {
            return p1[sortName] < p2[sortName] ? 1 : -1;
        });
    } else {
        arr.sort(function (p1, p2) {
            return p1[sortName] < p2[sortName] ? -1 : 1;
        });
    }
}
function subContent(content, num) {
    if (content.length > num) {
        return content.substring(0, num) + " ...";
    } else {
        return content;
    }

}